package NWish.properties.animator
{
	import NWish.Interface.ILogger;
	
	
	import NWish.Misc;
	import NWish.json.OrderDict;
	import NWish.properties.Properties;
	
	import away3d.animators.SmoothSkeletonAnimator;
	import away3d.animators.data.SkeletonAnimationSequence;
	import away3d.animators.data.SkeletonAnimationState;
	import away3d.containers.Scene3D;
	import away3d.entities.Mesh;
	
	
	public class SmoothSkeletonAnimatorProperties extends SkeletonAnimatorBaseProperties 
	{
		public function SmoothSkeletonAnimatorProperties(parent)
		{
			super(parent);
			
			/*var keys
			= 
				['play'
					,'addSequence'
				];
			addPropertieKeys('SmoothSkeletonAnimator',keys);*/
			
		}

		/*override public function get createEnable()
		{
			return true;
		}*/
		
		override public function toString()
		{
			return "SmoothSkeletonAnimator";
		}
		
		static public function hasAnimactor(obj)
		{
			if(obj.extra == null || obj.extra.smoothSkeletonAnimator == null)
				return false;
			
			return true;
		}
		static public function getAnimactorDict(dict)
		{
			return dict["SmoothSkeletonAnimator"];
		}
		
		public function hasAnimactor(obj)
		{
			return SmoothSkeletonAnimatorProperties.hasAnimactor(obj);
		}
		
		override public function propertiesToDict(obj,dict:OrderDict,read_only = false,saving = false)
		{
			super.propertiesToDict(obj.extra.smoothSkeletonAnimator,dict,read_only,saving);
			
			if(read_only == false)
			{
				if(saving == false)
				{
					if(obj.extra && obj.extra.meshFileName.search('.md5mesh') != -1)
					{
						if(obj.extra.animators)
							dict.push(Properties.readMark + 'AnimatorFiles', obj.extra.animators);
						
						
						dict.push('add Animator file', '');
						var anims = fileSystem.getFileNames('.md5anim');
						dict.push(Properties.readMark + 'Animator files', anims);
					}
				}
				else
				{
					if(obj.extra.animators)
						dict.push('AnimatorFiles', obj.extra.animators);
				}
			}
		}
		
		override public function dictToProperties(dict,obj,scene:Scene3D,isloading)
		{
			super.dictToProperties(dict,obj.extra.smoothSkeletonAnimator,scene,isloading);
			
			var controller:SmoothSkeletonAnimator = obj.extra.smoothSkeletonAnimator;
			
			var afs = dict['AnimatorFiles'];
			if(afs)//read from scene file
			{
				for each (var a in afs)
				{
					addAnim(a,controller,obj);
				}
				
			}
			else//read from edit json
			{
				var file = dict['add Animator file'];
				
				if(file == '' || file == null)
				{
					ILogger.log('please select a animator file!');
					return;
				}
				
				//
				if(obj.extra.animators)
				{
					for each (var a in obj.extra.animators)
					{
						if(a == file)
						{
							ILogger.log('animator file alreadly add in this mesh, anim file name is : ' + file,obj);
							return;
						}
					}
				}
				
				//
				addAnim(file,controller,obj);
			}

		}
		
		
		private function addAnim(file,controller,obj)
		{
			var seq:SkeletonAnimationSequence = fileSystem.getResource(file);
			
			if(seq)
			{
				controller.addSequence(seq);
				controller.play(seq.name, 0);
				controller.updateRootPosition = false;
				
				if(obj.extra.animators == null)
					obj.extra.animators = [];
				obj.extra.animators.push(file);
			}
		}
		
		public function simplePropertiesToDict(obj,dict:OrderDict)
		{
			var controller:SmoothSkeletonAnimator = obj.extra.smoothSkeletonAnimator;
			dict.push('SmoothSkeletonAnimator', controller.name);
		}
	}
}